package zzs.generalframework.console.core.parameter.commonparameter.jobtitle.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.jobtitle.JobTitleBO;
import zzs.generalframework.common.core.parameter.commonparameter.jobtitle.JobTitleInfo;
import zzs.generalframework.common.core.parameter.commonparameter.jobtitle.JobTitlePO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.jobtitle.dao.IJobTitleDao;
import zzs.generalframework.console.core.parameter.commonparameter.jobtitle.service.IJobTitleService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.jobtitle.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.jobtitle.service.impl.jobTitleServiceImpl")
public class JobTitleServiceImpl implements IJobTitleService
{

	private Logger logger = LoggerFactory.getLogger(JobTitleServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IJobTitleDao jobTitleDao;

	@Override
	public ConsoleReturnBO saveJobTitle(JobTitleBO jobTitleBO)
	{
		logger.debug("====保存职称信息开始====");
		logger.debug("====保存职称信息.入参:JobTitleBO=[{}]====", jobTitleBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		JobTitlePO jobTitlePO = new JobTitlePO();
		copyJobTitleBO2JobTitlePO(jobTitleBO, jobTitlePO);
		Boolean insertResult = jobTitleDao.insert(jobTitlePO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存职称信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存职称信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveJobTitle(List<JobTitleBO> jobTitleBOList) throws ConsoleException
	{
		logger.info("====保存职称信息列表开始====");
		logger.debug("====保存职称信息列表.入参:JobTitleBOList=[{}]====", jobTitleBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (JobTitleBO itemBO : jobTitleBOList)
		{
			ConsoleReturnBO itemResult = saveJobTitle(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存职称信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存职称信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getJobTitle(JobTitleInfo jobTitleInfo)
	{
		logger.info("====获取职称信息开始====");
		logger.debug("====获取职称信息.入参:JobTitleInfo=[{}]====", jobTitleInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<JobTitlePO> jobTitlePOList = jobTitleDao.select(jobTitleInfo);
		if (jobTitlePOList == null || jobTitlePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (jobTitlePOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			JobTitleBO jobTitleBO = new JobTitleBO();
			copyJobTitlePO2JobTitleBO(jobTitlePOList.get(0), jobTitleBO);
			returnConsoleReturnBO.setReturnObj(jobTitleBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			JobTitleBO jobTitleBO = new JobTitleBO();
			copyJobTitlePO2JobTitleBO(jobTitlePOList.get(0), jobTitleBO);
			returnConsoleReturnBO.setReturnObj(jobTitleBO);
		}
		logger.debug("====获取职称信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取职称信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listJobTitle(JobTitleInfo jobTitleInfo)
	{
		logger.info("====获取职称信息列表开始====");
		logger.debug("====获取职称信息列表.入参:JobTitleInfo=[{}]====", jobTitleInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<JobTitlePO> jobTitlePOList = jobTitleDao.select(jobTitleInfo);
		if (jobTitlePOList == null || jobTitlePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<JobTitleBO> resultList = new ArrayList<JobTitleBO>();
			for (JobTitlePO itemPO : jobTitlePOList)
			{
				JobTitleBO itemBO = new JobTitleBO();
				copyJobTitlePO2JobTitleBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取职称信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取职称信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateJobTitle(JobTitleBO jobTitleBO)
	{
		logger.info("====更新职称信息开始====");
		logger.debug("====更新职称信息.入参:JobTitleBO=[{}]====", jobTitleBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		JobTitlePO jobTitlePO = new JobTitlePO();
		copyJobTitleBO2JobTitlePO(jobTitleBO, jobTitlePO);
		Boolean updateResult = jobTitleDao.update(jobTitlePO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新职称信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新职称信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateJobTitle(List<JobTitleBO> jobTitleBOList) throws ConsoleException
	{
		logger.info("====更新职称信息列表开始====");
		logger.debug("====更新职称信息列表.入参:List<JobTitleBO>=[{}]====", jobTitleBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (JobTitleBO itemBO : jobTitleBOList)
		{
			ConsoleReturnBO itemResult = updateJobTitle(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新职称信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新职称信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeJobTitle(JobTitleBO jobTitleBO)
	{
		logger.info("====删除职称信息开始====");
		logger.debug("====删除职称信息.入参:JobTitleBO＝[{}]======", jobTitleBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		JobTitlePO jobTitlePO = new JobTitlePO();
		copyJobTitleBO2JobTitlePO(jobTitleBO, jobTitlePO);
		Boolean deleteResult = jobTitleDao.delete(jobTitlePO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除职称信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除职称信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeJobTitle(List<JobTitleBO> jobTitleBOList) throws ConsoleException
	{
		logger.info("====删除职称信息列表开始====");
		logger.debug("====删除职称信息列表.入参:List<JobTitleBO>＝[{}]======", jobTitleBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (JobTitleBO itemBO : jobTitleBOList)
		{
			ConsoleReturnBO itemResult = removeJobTitle(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除职称信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除职称信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countJobTitle(JobTitleBO jobTitleBO)
	{
		logger.info("====统计职称信息开始====");
		logger.debug("====统计职称信息。入参：JobTitleBO＝[{}]======", jobTitleBO);
		Long returnLong = new Long(0);
		JobTitlePO jobTitlePO = new JobTitlePO();
		copyJobTitleBO2JobTitlePO(jobTitleBO, jobTitlePO);
		returnLong = jobTitleDao.count(jobTitlePO);
		logger.debug("====统计职称信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计职称信息结束====");
		return returnLong;
	}

	private void copyJobTitleBO2JobTitlePO(JobTitleBO jobTitleBO, JobTitlePO jobTitlePO)
	{
		beanUtils.copyBeanNotNull(jobTitleBO, jobTitlePO);
		if (jobTitleBO.getSysID() != null)
		{
			jobTitlePO.setSeqID(jobTitleBO.getSysID());
		}
	}

	private void copyJobTitlePO2JobTitleBO(JobTitlePO jobTitlePO, JobTitleBO jobTitleBO)
	{
		beanUtils.copyBeanNotNull(jobTitlePO, jobTitleBO);
		if (jobTitlePO.getSeqID() != null)
		{
			jobTitleBO.setSysID(jobTitlePO.getSeqID());
		}
	}

}
